<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Auctions\Auction;
use App\Models\Auctions\Zhuanchang;
use App\Models\Auctions\Art;
use App\Models\Area;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
class SynchroAuction extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'synchro:auction';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        
		// for($i=1;$i<350;$i++)
		// {
		// 	if(!$this->downpic($i))
		// 	{
		// 		break;
		// 	}
		// }
		
		// $this->auction(1);
		// return $this->arts(1);
		// $this->zhuanchang(1);
        //执行更新排名首页数据
		$this->auction_index();
		
		
    }
	
	public function downpic($i)
	{
		$list = Art::where('thumb', 'like', 'http://download.zhuokearts.com/Auction%')->get();
		if($list->isEmpty())return false;
		foreach($list as $row)
		{
			
		}
	}
	public function auction_index()
	{
		$minutes = 10080;
		//本年度单品排行
		$key = 'now_year_auction_index_arts';
		//->where('sale_time', 'like', date('Y').'%')
		$list = Art::where('status', 1)->where('price_rmb', '>', 0)->orderBy('price_rmb', 'desc')->limit(10)->get();
		
		Cache::put($key, $list, $minutes);
		Redis::set($key, $list);
		//拍卖公司总成交额排行榜
		$key = 'now_year_auction_index_company';
		$list = [];
		//->where('sale_time', 'like', date('Y').'%')
		$list = Art::where('status', 1)->select(\DB::raw('sum(price_rmb) as price_rmb, company_id'))->where('sale_time', '>', 0)->groupBy('company_id')->get();
		
		$list = $list->sortByDesc('price_rmb')->forPage(1,10);
		Cache::put($key, $list, $minutes);
		Redis::set($key, $list);
		//拍卖专场成交额排行
		$key = 'now_year_zhuanchang_index_arts';
		$list = Zhuanchang::where('status', 1)->where('is_show', 1)->where('start_time', 'like', date('Y').'%')->orderBy('sum_all', 'desc')->limit(10)->get();
		// dd($list);
		Cache::put($key, $list, $minutes);
		Redis::set($key, $list);
		// dd($list);
		
	}
    public function zhuanchang($page)
    {
        $client = new \GuzzleHttp\Client();
		$info = Zhuanchang::orderBy('id', 'desc')->first();
        $res = $client->request('GET', 'http://apitest.zhoukearts.com/zhuanchang?id='.$info->id);
        $ret = json_decode($res->getBody());
        
        if(empty($ret->data))return;
        $attr = $artist = [];
        foreach($ret->data as $k=>$row)
        {
            $c = substr($row->userdate,0,19);
            if($c == '1900-01-01 00:00:00' || empty($c) || $c<'1990-01-01 00:00:00')
            {
                $c = null;
            }
            $attr = [
                'id' => $row->id,
                'listorder' => $row->xuhao,
                'auctions_id' => $row->pmh_id,
                'status' => $row->aucnews_flag,
                'is_show' => $row->cnflag,
                'title' => $row->title,
				'start_time' => $c,
				'content' => $row->content,
				'sum_all' => $row->sum_all,
				'onclick' => $row->viewcount,
				'rate' => $row->rate,
				'bflag' => $row->bflag,
				'prevflag' => $row->prevflag,
				'price_g_name' => $row->price_g_name,
				'hkb_isshow' => $row->HKBisshow,
				'usd_isshow' => $row->USDisshow,
				'gbp_isshow' => $row->GBPisshow,
				'iszhibo' => $row->iszhibo,
				'thumb' => $row->pic,
				'auction_area' => $row->auction_area,
				'index_recommed' => $row->recommendtoindex,
				'class_id' => $row->CLASS_ID,
                
                'created_at' => $c,
                
            ];
            // dd($attr);
            if(!Zhuanchang::find($row->id))Zhuanchang::insert($attr);
            
            
        }
        // dd($attr);
        echo $page;
        
        //dd($page);
        sleep(1);
    }

    public function auction($page)
    {
        $client = new \GuzzleHttp\Client();
		$info = Auction::orderBy('id', 'desc')->first();
        $res = $client->request('GET', 'http://apitest.zhoukearts.com/auction?id='.$info->id);
        $ret = json_decode($res->getBody());
        
        if(empty($ret->data))return;
        $attr = $artist = [];

        $Area = Area::get();
        $areas = [];
        foreach ($Area as $key => $value) {
            # code...
            $areas[$value->id] = $value;
        }

        foreach($ret->data as $k=>$row)
        {
            $c = substr($row->userdate,0,19);
            if($c == '1900-01-01 00:00:00' || empty($c) || $c<'1990-01-01 00:00:00')
            {
                $c = null;
            }
            $prov_id = 0;
            if(array_key_exists($row->AREA_ID, $areas))
            {
                if($areas[$row->AREA_ID])
                {
                    $prov_id = $areas[$row->AREA_ID]->parent_id;
                }
            }
            if($prov_id==0)$prov_id = $row->AREA_ID;
            $attr = [
                'id' => $row->id,
                'listorder' => $row->xuhao,
                'status' => $row->aucnews_flag,
                'is_show' => $row->cnflag,
                'is_show_zc' => $row->isshow,
                'title' => $row->title,
                'keyno' => $row->keyno,
                'content' => $row->content,
                'preview_start_time' => $row->userdate,
                'preview_end_time' => $row->preview_end,
                'start_time' => $row->endtime,
                'end_time' => $row->result_end,
                'city' => $row->city,
                'company_id' => $row->corpno,
                'onclick' => $row->viewcount,
                'work_flag' => $row->work_flag,
                'sum_all' => $row->sum_all,
                'rate' => $row->rate,
                'tjflag' => $row->tjflag,
                'iszhibo' => $row->iszhibo,
                'area_id' => $row->AREA_ID,
                'preview_address' => $row->PREVIEW_AREA,
                'address' => $row->RESULT_AREA,
                'prov_id' => $prov_id,
                'search_title' => $row->SEARCHTITLE,
                
                'created_at' => $c,
                
            ];
            // dd($attr);
            if(!Auction::find($row->id))Auction::insert($attr);
            
            
        }
        // dd($attr);
        echo $page;
        
        //dd($page);
        sleep(1);
    }
	public function arts($page)
    {
        $client = new \GuzzleHttp\Client();
		$info = Art::orderBy('id', 'desc')->first();
        $res = $client->request('GET', 'http://apitest.zhoukearts.com/artss?id='.$info->id);
        $ret = json_decode($res->getBody());
        
        if(empty($ret->data))return;
        $attr = $artist = [];
        foreach($ret->data as $k=>$row)
        {
			
            
            
            $attr = [
                'id' => $row->id,
                'price_rmb' => $row->price_rmb,
                'price_hkd' => $row->price_hkd,
                'price_usd' => $row->price_usd,
                'price_gbp' => $row->price_gbp,
                
                
                
                
            ];
            // dd($attr);
			$info = Art::find($row->id);
            if($info)
			{
				$info->price_rmb = $row->price_rmb;
				$info->price_hkd = $row->price_hkd;
				$info->price_usd = $row->price_usd;
				$info->price_gbp = $row->price_gbp;
				$info->save();
				echo $info->id;
			}
            
            
        }
        // dd($attr);
        echo $page;
        
        //dd($page);
        sleep(1);
    }
	
	public function updateBatch($tableName = "", $multipleData = array()){
 
        if( $tableName && !empty($multipleData) ) {
 
            // column or fields to update
            $updateColumn = array_keys($multipleData[0]);
            $referenceColumn = $updateColumn[0]; //e.g id
            unset($updateColumn[0]);
            $whereIn = "";
 
            $q = "UPDATE ".$tableName." SET "; 
            foreach ( $updateColumn as $uColumn ) {
                $q .=  $uColumn." = CASE ";
 
                foreach( $multipleData as $data ) {
                    $q .= "WHEN ".$referenceColumn." = ".$data[$referenceColumn]." THEN '".$data[$uColumn]."' ";
                }
                $q .= "ELSE ".$uColumn." END, ";
            }
            foreach( $multipleData as $data ) {
                $whereIn .= "'".$data[$referenceColumn]."', ";
            }
            $q = rtrim($q, ", ")." WHERE ".$referenceColumn." IN (".  rtrim($whereIn, ', ').")";
 
            // Update  
            return \DB::update(\DB::raw($q));
 
        } else {
            return false;
        }
	}
}
